//
//  CollectionMainViewController.swift
//  SafeFoodManagerDemo
//
//  Created by 振轩 on 2018/4/17.
//  Copyright © 2018年 bob. All rights reserved.
//

import UIKit
let titles = ["文章","视频"]
class CollectionMainViewController: UIViewController {
    
    
    var btn:UIButton?
    
    var currentIndex:Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
        navigationItem.title = "我的收藏"
        addEditBtn()
        
        NotificationCenter.default.addObserver(self, selector: #selector(changeCurrentIndex(notify:)), name: NSNotification.Name(rawValue: "currentIndexChange"), object: nil)
        
    }
    
    @objc func changeCurrentIndex(notify:NSNotification){
        
        currentIndex =   notify.userInfo!["currentIndex"] as! Int
        
        print("____$$$$$$$$")
        print(currentIndex)
        print("____$$$$$$$$")
    }
    
    
    func setUpUI(){
        
        //创建主题内容。
        let style = ZXTitleStytle()
        style.isScrollEnable = false
        
        let pageFrame = CGRect(x: 0, y: kNavigationBarH + kStatusBarH-20, width: kScreenW, height: kScreenH - kNavigationBarH - kStatusBarH)
        let vc2 = CollVideoTableViewController()
     
        //vc2.isdeleMore = true
        //vc2.deleStatue = true
        
        let childVcs:[UIViewController] = [CollArticleTableViewController(),  vc2]
        
        
        let pageView = ZXPageView(frame: pageFrame, titles: titles, style: style, childVcs: childVcs, parentVc: self)
        
        
    
        view.addSubview(pageView)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func  addEditBtn(){
        
        btn = UIButton.init(type: .system)
        btn?.frame = CGRect(x: 0, y: 0, width: 60, height: 20)
        btn?.setTitle("编辑", for: .normal)
        btn?.setTitle("完成", for: .selected)
        btn?.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        
        btn?.sizeToFit()
        btn?.tintColor = UIColor.clear
        btn?.setTitleColor(.white, for: .normal)
        btn?.setTitleColor(.white, for: .selected)
        
        let item = UIBarButtonItem.init(customView: btn!)
        btn?.addTarget(self, action: #selector(gotoEdit), for: .touchUpInside)
        self.navigationItem.rightBarButtonItem = item
        
    }
    
    @objc func gotoEdit(sender:UIButton){
        
        sender.isSelected = !sender.isSelected
        if (self.btn?.isSelected)! {
          NotificationCenter.default.post(name: NSNotification.Name.init("editor"), object: self, userInfo: ["target":currentIndex])
        }else{
             NotificationCenter.default.post(name: NSNotification.Name.init("cancel"), object: self, userInfo: ["target":currentIndex])
            
        }
        
        
    }


   

}
